import { fundFlowType, getYuanGong } from '@/utils/helper';
import { useEffect, useRef, useState } from 'react';
import { ProTable } from '@ant-design/pro-components';
import { isSuccess, setDefEmployeeId } from '@/utils';
import { fundFlowApi, userListApi } from '@/api';
import dayjs from 'dayjs';
import { createTimeLie, yuanGongLie } from './ComonOrder';

export default function FundFlow() {
  const tableRef: any = useRef('');
  const [yuanGongs, setYuanGongs] = useState<any>([]);
  const [defYuanGongId, setDefYuanGongId] = useState<any>('');

  useEffect(() => {
    initYuanGong();
  }, []);

  // 资金流水
  async function request(e: any) {
    const params: any = {
      ...e,
      contract: '',
      userIds: [e.yuanGongId],
    };
    if (e?.type === 'all') delete params.type;
    console.log('params: ', params);
    const res: any = await fundFlowApi(params);
    if (!isSuccess(res)) return { data: [], total: 0 };
    const _list = res.data.map((item: any, i: number) => ({ ...item, idOld: item.id, id: i + 1 }));
    console.log('_list: ', _list);
    return { data: _list, total: res?.data?.total, success: true };
  }

  const columns: any[] = [
    {
      title: '时间',
      dataIndex: 'time',
      render: (text: any) => <>{dayjs(text * 1000).format('YYYY-MM-DD HH:mm:ss')}</>,
      hideInSearch: true,
    },
    createTimeLie(true),
    yuanGongLie(yuanGongs, defYuanGongId),
    {
      title: '金额',
      dataIndex: 'change',
      hideInSearch: true,
      render: (text: any) => (
        <p className={`${text > 0 ? 'color-red' : 'color-green'}`}>{text} USDT</p>
      ),
    },
    {
      title: '账户余额',
      dataIndex: 'balance',
      hideInSearch: true,
    },
    {
      title: '操作类型',
      dataIndex: 'type',
      render: (text: any) => <p>{fundFlowType[text]}</p>,
      hideInSearch: true,
    },
    {
      title: '合约',
      dataIndex: 'contract',
      hideInSearch: true,
    },
    {
      title: '备注',
      dataIndex: 'text',
      hideInSearch: true,
    },
  ];

  // 员工列表
  const initYuanGong = async () => {
    const yuanGongs: any = await getYuanGong(userListApi);
    setYuanGongs(yuanGongs);
    setDefYuanGongId(setDefEmployeeId(yuanGongs));
  };

  return (
    <>
      {yuanGongs && Object.keys(yuanGongs).length > 0 && (
        <ProTable
          rowKey="id"
          request={request}
          columns={columns}
          actionRef={tableRef}
          options={false}
          scroll={{ x: 1200 }}
        />
      )}
    </>
  );
}
